#include "Dictionary.h"
#include "MyMap.h"
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
using namespace std;

#define MaxWordLength 30 //max length of word
CDictionary WordDic;
//http://www.rosoo.net/a/201011/10509.html
void WordsMap(char *str)
{
	FindMapWord(str);
}

void SegmentSentence(char *str)
{
	printf("called SegmentSentence\n");
	printf("*str=%s\n",str);
	int ini_len=strlen(str);
	
	char * s1=(char *)malloc(ini_len+1);
	strcpy(s1,str);
	
	char *word_buf=(char *)malloc(ini_len);
	while(strlen(s1)!=0)//string is not null
	{
		printf("enter while\n");
		
		memset(word_buf,0,ini_len);// initializion 
		printf("*s1=%s\n",s1);
		int len = strlen(s1); 
		printf("len=%d\n",len);
		
		if (len > MaxWordLength) // length of string > MaxWordLength 
		{
			len = MaxWordLength; 
		}
		
		strncpy(word_buf,s1+strlen(s1)-len,len);
		
		printf("*w=%s\n",word_buf);
		int n = WordDic.FindWord(word_buf); // find word_buf word in WordDic	
		while(len > 3 && n == 0) // not find word_buf and len >3
		{
			printf("not find\n");
			len -= 3; 
			printf("len = %d\n",len);
			memset(word_buf,0,ini_len);
			strncpy(word_buf,s1+strlen(s1)-len,len);
			printf("**word_buf=%s\n",word_buf);
			n = WordDic.FindWord(word_buf);//find word_buf word in WordDic	
		}	
		/*
		*if the word_buf can find in WordDic, then insert the word_buf into the WordMap
		*/
		if (n==1)
		{
			printf("n==1,WordsMap\n");
			WordsMap(word_buf);
		}
	
		strncpy(word_buf,s1,strlen(s1)-len);//discard the lastest len char
		printf("*w=%s\n",word_buf);
		
		if((strlen(s1)-len)!=0)
		{
			printf("s1 = %s\n\n\n",s1);
			strcpy(s1,word_buf);
			//sleep(1);
		}
		else
		{	
			memset(s1,0,strlen(s1));
			printf("current string  process finished...\n");
		}
	}
	
	free(s1);
	free(word_buf);
}


void SegmentSentenceMM (char *str)
{
	printf("calling SenmentSentenceMM\n");
	printf("*str=%s\n",str);
	int i;
	int dd;
	int ini_len=strlen(str);
	char *s1=(char *)malloc(ini_len+1);
	strcpy(s1,str);
	char * tmp=(char *)malloc(ini_len+1);

	while(strlen(s1) != 0)
	{
		unsigned char ch = (unsigned char)s1[0];
		if (ch < 128) // English punctuation
		{
			printf("calling ch<128\n");
			i = 1;
			dd = strlen(s1);
			while (i < dd && ((unsigned char)s1[i] < 128) && (s1[i] != 10) && (s1[i] != 13)) //LR ,CF.Line breaks and spaces
			{
				i++;
			}
			strncpy(tmp,s1+i,dd);
			strcpy(s1,tmp);
			continue;
		}
		else
		{
			printf("calling ch>128\n");
			if (ch < 176) // chinese punctuation
			{
				printf("ch<176\n");
				i = 0;
				dd = strlen(s1);
				while(i < dd && ((unsigned char)s1[i] < 176) && ((unsigned char)s1[i] >= 161)
					&& (!((unsigned char)s1[i] == 161 && ((unsigned char)s1[i+1] >= 162 && (unsigned char)s1[i+1] <= 168)))
					&& (!((unsigned char)s1[i] == 161 && ((unsigned char)s1[i+1] >= 171 && (unsigned char)s1[i+1] <= 191)))
					&& (!((unsigned char)s1[i] == 163 && ((unsigned char)s1[i+1] == 172 || (unsigned char)s1[i+1] == 161)
					|| (unsigned char)s1[i+1] == 168 || (unsigned char)s1[i+1] == 169 || (unsigned char)s1[i+1] == 186
					|| (unsigned char)s1[i+1] == 187 || (unsigned char)s1[i+1] == 191)))
				{
					i = i + 3; // Each character three bytes
				}
				if (i == 0)
				{
					i = i +3;
				}
				strncpy(tmp,s1+i,dd);
				strcpy(s1,tmp);
				printf("*s1=%s\n",s1);
				continue;
			}
			printf("ch>=176\n");
		}
		i = 3;
		dd = strlen(s1);

		printf("*s1=%s,and len is %d\n",s1,dd);
		while(i < dd && (unsigned char)s1[i] >= 176)
		{
			i += 3;
		}
		printf("*s1=%s\n",s1);
		printf("i=%d\n",i);
		
		memset(tmp,0,ini_len+1);
		memcpy(tmp,s1,i);
		printf("*tmp=%s\n",tmp);
		printf("calling SegmentSentence\n");
		SegmentSentence(tmp);
		strncpy(tmp,s1+i,dd);
		strcpy(s1,tmp);
		
		if(strlen(s1)!=0)
			printf("in SegMM,*s1=%s\n",s1);
	}
	
	
	free(s1);
	free(tmp);
}
int main(int argc,char **argv )
{
	/*
	*Initialization,include file directory,FILE pointer,buffer
	*/
	DIR *directory_pointer; //DIRÊÇdirectoryµÄËõÐŽ£¬ÊÇÄ¿ÂŒµÄÒâËŒ
	struct dirent *entry;
	FILE *fp = NULL;
	char buffer[256]={'\0'};
	
	/*
	*test for when input is char*...
	*/	
	//char * mytmp="我们的爱错过就不该直到现在我才想起来啊";
	//SegmentSentenceMM(mytmp);	
				 			

	/*
	*when input is folder,Execution: ./lab1 test
	*/
	if (argc!=2)
	{
		printf("Must specify a directory\n");
    	exit(1);
  	}
  	ClearMapWord();
	if ((directory_pointer=opendir(argv[1]))==NULL)
    	printf("Error opening %s\n",argv[1]);
	else
	{
		while ((entry=readdir(directory_pointer))!=NULL)
    	{ 
      		if(strcmp(entry->d_name,".")==0||strcmp(entry->d_name,"..")==0)
      		{
				continue ;		
			}
			else
			{
				char path[50]="./test/";
				strcat(path,entry->d_name);//the file of path is ./test/test1.txt or ./test/test2.txt...
				printf("path=%s\n",path);
				fp = fopen(path,"rb");
      			if(fp == NULL)
    			{
        			printf("cannot open this file!\n");
        			continue;
    			}
      			while(!feof(fp))
      			{
					if(fgets(buffer,256,fp) == NULL)
           				break;
        			buffer[strlen(buffer)-1] = '\0';	
					printf("*buffer=%s\n",buffer);	
					SegmentSentenceMM(buffer);//calling SegmentSentenceMM function Processes punctuation
				}			
			}
    	}
		closedir(directory_pointer);//close folder directory_pointer
  	}	
 

	sortMapByValue(mapword,tVector); //sort by second(value),store the tVector Descending

	//FindWord("我们");//find a word whether is in WordDic or not
	PrintfMap();
	MapWordSize();

	return 0;
}
